package com.example.demo.util;

import com.example.demo.config.MessageConfig;
import com.example.demo.config.StatusType;

import java.util.*;

/**
 * 谁是卧底工具类
 */
public class SSWDUtil {

    //储存房间号和房间信息
    private static final Map<String,SSWDRoom> map = new HashMap<>();

    //用户和房间号的对照
    private static final Map<String,String> userAndRoomMap = new HashMap<>();

    public static String start(String userId) {
        //调整改用户状态为 成语接龙状态
        CacheUtil.set(userId, StatusType.SSWD);
        //提示用户想要进行什么操作
        voteMap.put(userId,MessageConfig.SSWD_MESSAGE_START);
        return MessageConfig.SSWD_MESSAGE_START;
    }

    public static String createRoom(String userId) {
        SSWDRoom sswdRoom = new SSWDRoom();
        sswdRoom.setRoomOwner(userId);
        UUID uuid = UUID.randomUUID();
        String room = uuid.toString().substring(0, 6);
        sswdRoom.setRoomID(room);
        List<String> players = sswdRoom.getPlayers();
        players.add(userId);
        //将用户和房间号存入 userAndRoomMap 中
        userAndRoomMap.put(userId,room);
        //将房间号和房间信息存入 map 中
        map.put(room, sswdRoom);
        //设置刷新的消息
        String msg = sswdRoom+"\n按s刷新内容继续操作" +
                "\n按1准备" +
                "\n将房间号分享给小伙伴加入房间";
        voteMap.put(userId,msg);
        return msg;
    }

    public static String joinRoom(String toUserName, String room) {
        SSWDRoom sswdRoom = map.get(room);
        if(sswdRoom==null){
            throw new RuntimeException("加入房间失败,房间号:" + room);
        }
        List<String> players = sswdRoom.getPlayers();
        players.add(toUserName);
        userAndRoomMap.put(toUserName,room);

        String msg = sswdRoom+"\n按s刷新内容继续操作" +
                "\n按1准备" +
                "\n将房间号分享给小伙伴加入房间";
        voteMap.put(toUserName,msg);
        return msg;
    }


    //玩家准备 如果全部准备 等待房主开始游戏
    public static String ready(String userId) {
        SSWDRoom sswdRoom = map.get(userAndRoomMap.get(userId));
        sswdRoom.getReadPlayers().add(userId);
        String msg = sswdRoom+"\n按s刷新内容继续操作" +
                "\n按1准备" +
                "\n将房间号分享给小伙伴加入房间";
        voteMap.put(userId,msg);
        //判断 是否所有人都准备完毕
        int size = sswdRoom.getReadPlayers().size();
        if(size==sswdRoom.getPlayers().size()){
            //开始游戏 操作一下房间 给每个人 分配一个词
            sswdRoom.setStatus("准备完毕等待房主开始游戏");
            //给每个人设置消息
            for (String player : sswdRoom.getPlayers()) {

                voteMap.put(player,"等待房主开始游戏");
            }
            String roomOwner = sswdRoom.getRoomOwner();
            voteMap.put(roomOwner,"请按2开始游戏");
        }
        return msg;
    }

    public static boolean cancelReady(String userId) {
        SSWDRoom sswdRoom = map.get(userAndRoomMap.get(userId));
        sswdRoom.getReadPlayers().remove(userId);
        return true;
    }

    public static String refresh(String userId) {
        SSWDRoom sswdRoom = map.get(userAndRoomMap.get(userId));
        //判断 是否所有人都准备完毕
        int size = sswdRoom.getReadPlayers().size();
        String string = sswdRoom.toString();
        if(size==sswdRoom.getPlayers().size()&&sswdRoom.getWordList()==null){
            //开始游戏 操作一下房间 给每个人 分配一个词
            sswdRoom.setStatus("游戏中");
            //随机分配词语
            List<String> strings = new ArrayList<>();
            for (int i = 0; i < size-1; i++) {
                strings.add("啤酒");
            }
            //随机数 0-3
            Random random = new Random();
            int i = random.nextInt(size-1);
            strings.add(i,"黄酒");
            sswdRoom.setWordList(strings);
        }
        if(sswdRoom.getWordList() != null){
            string+="词语分配完毕\n" +
                    "您的词语是:"+sswdRoom.getWordList().get(sswdRoom.getPlayers().indexOf(userId))+"\n" +
                    "您需要描述这个词语,并且不被别人投票出局,请回复描述词,并且按s刷新查看进度\n";
            CacheUtil.set(userId+"_sswd","等待回复描述词");
        }
        return string;
    }

    public static void setDescribe(String userId, String content) {
        SSWDRoom sswdRoom = map.get(userAndRoomMap.get(userId));
        List<String> readPlayers = sswdRoom.getReadPlayers();
        Map<String, String> cacheWordMap = sswdRoom.getCacheWordMap();
        for (int i = 0; i < readPlayers.size(); i++) {
            if(userId.equals(readPlayers.get(i))){
                cacheWordMap.put(i+"号",content);
                break;
            }
        }
    }

    public static String refreshDescribe(String toUserName) {
        SSWDRoom sswdRoom = map.get(userAndRoomMap.get(toUserName));
        Map<String, String> cacheWordMap = sswdRoom.getCacheWordMap();
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("当前描述词如下:\n");
        for (String s : cacheWordMap.keySet()) {
            stringBuffer.append(s+" : "+cacheWordMap.get(s)+"\n");
        }
        return stringBuffer.toString();
    }


    private static Map<String,String> voteMap = new HashMap<>();

    public static String getMessage(String toUserName) {
        return voteMap.get(toUserName);
    }

    public static String startGame(String userId) {
        SSWDRoom sswdRoom = map.get(userAndRoomMap.get(userId));
        int size = sswdRoom.getReadPlayers().size();
        if(size==sswdRoom.getPlayers().size()){
            //开始游戏 操作一下房间 给每个人 分配一个词
            sswdRoom.setStatus("游戏中");
            //随机分配词语
            List<String> wordList = sswdRoom.getWordList();
            for (int i = 0; i < size-1; i++) {
                wordList.add("啤酒");
                voteMap.put(userId,"啤酒");
            }
            //随机数 0-3
            Random random = new Random();
            if(size <= 1){
                wordList.add(0,"黄酒");
            }else {
                int i = random.nextInt(size-1);
                wordList.add(i,"黄酒");
            }
            voteMap.put(userId,"黄酒");

        }
        String msg = sswdRoom+"\n按s刷新内容继续操作" +
                "\n词语分配完毕\n" +
                "您需要描述这个词语,并且不被别人投票出局,请回复描述词\n";

        return msg;
    }
}
